﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class DanhMuc_DSDoanVienGioiThieuDang : System.Web.UI.Page
{
    public static string donvi = "";
    readonly SecurityLib.Sec_AES _se = new SecurityLib.Sec_AES();
    public string st = "";
    _dbDataContext _db = new _dbDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        donvi = _se.Decrypt(Request.QueryString["dvid"].Replace(" ", "+"), "gold", 128); //MemberShip_ASP.GetMaDonViFromUserName(Page.User.Identity.Name);
        if (!IsPostBack)
        {
            BindGrid();
            st = _se.Decrypt(Request.QueryString["tid"].Replace(" ", "+"), "gold", 128);
        }
    }
    protected void btn_timkiem_Click(object sender, EventArgs e)
    {
        BindGrid();
    }
    protected void Btn_xuatfile_Click(object sender, EventArgs e)
    {
        Session.Remove("ListMaDoanVien_Print");
        Session.Remove("TenDonVi_Print");
        string listmadoanvien = "";
        CheckBox ckh;
        Label lbl;
        int count = 0;
        foreach (GridViewRow dr in Grid_DSDVDang.Rows)
        {
            ckh = (CheckBox)dr.FindControl("chkchon");
            if (ckh.Checked == true)
            {
                lbl = (Label)dr.FindControl("lblMaDoanVien");
                listmadoanvien += lbl.Text.Trim() + "!";
                count++;
            }
        }
        if (count > 0)
        {
            Session["ListMaDoanVien_Print"] = listmadoanvien;
            Session["TenDonVi_Print"] = _se.Decrypt(Request.QueryString["tid"].Replace(" ", "+"), "gold", 128);
            Response.Redirect("javascript:window.open('../Print/PrintPage.aspx','_blank');");
        }
        else
        {
            SecurityLib.ToolsMore.ShowPopUpMsg("Chưa chọn đoàn viên để in.", this.Page);
        }
    }
    //chọn all checkbox
    protected void chkInGridviewHeader_CheckedChanged(object sender, EventArgs e)//day la ham de goi trong header cua itemtemplate
    {
        CheckBox chk = (CheckBox)sender;
        CheckBox chkitem;
        foreach (GridViewRow dr in Grid_DSDVDang.Rows)
        {
            chkitem = (CheckBox)dr.FindControl("chkchon");
            if (chk.Checked == true)
            {
                chkitem.Checked = true;
            }
            else
            {
                chkitem.Checked = false;
            }
        }
    }
    //autocomplete by name
    [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
    public static string[] GetCompletionList_byname(string prefixText)
    {
        _dbDataContext _db = new _dbDataContext();
        var val = (from p in _db.tblDoanViens.Where(r => r.TrucThuoc.Trim() == donvi && r.MaDonVien.Trim()==r.tblGioiThieuChoDang.MaDoanVien.Trim() && r.HoTen.Trim().Contains(prefixText)) select p).ToList();

        string[] items = new string[val.ToList().Count];
        int i = 0;
        foreach (var item in val)
        {
            items.SetValue(item.HoTen.Trim(), i);
            i++;
        }
        return items;
    }
    //load du lieu vao gridview
    private void BindGrid()
    {
        if (txtHoten.Text.Length == 0)
        {
            var val = from p in _db.tblDoanViens
                      where p.TrucThuoc == donvi && p.MaDonVien.Trim() == p.tblGioiThieuChoDang.MaDoanVien.Trim()
                      select new
                      {
                          MaDoanVien=p.MaDonVien.Trim(),
                          TenDoanVien=p.HoTen.Trim(),
                          Phai=(p.Phai==true?"Nam":"Nữ"),
                          NgaySinh=string.Format("{0:dd/MM/yyyy}",p.NgaySinh),
                          CMND=p.CMND,
                          NgayGioiThieu=string.Format("{0:dd/MM/yyyy}",p.tblGioiThieuChoDang.NgayGioiThieu),
                          KetNapDang=p.tblGioiThieuChoDang.DuocKetNapDang,
                          NgayKetNapDang = (p.tblGioiThieuChoDang.DuocKetNapDang == true ? p.tblGioiThieuChoDang.NgayKetNapDang.ToString() : ""),
                          HuyKetNap=(p.tblGioiThieuChoDang.DuocKetNapDang==true?false:true)
                      };
            Grid_DSDVDang.DataSource = val.ToList();
            Grid_DSDVDang.DataBind();
        }
        else
        {
            var val=from p in _db.tblDoanViens where p.TrucThuoc == donvi && p.HoTen.Contains(txtHoten.Text) select new {
                MaDoanVien=p.MaDonVien.Trim(),
                TenDoanVien=p.HoTen.Trim(),
                Phai=(p.Phai==true?"Nam":"Nữ"),
                NgaySinh=string.Format("{0:dd/MM/yyyy}",p.NgaySinh),
                CMND=p.CMND,
                NgayGioiThieu=string.Format("{0:dd/MM/yyyy}",p.tblGioiThieuChoDang.NgayGioiThieu),
                KetNapDang=p.tblGioiThieuChoDang.DuocKetNapDang,
                NgayKetNapDang = (p.tblGioiThieuChoDang.DuocKetNapDang == true ? p.tblGioiThieuChoDang.NgayKetNapDang.ToString() : ""),
                HuyKetNap = (p.tblGioiThieuChoDang.DuocKetNapDang == true ? false : true)
            };
            Grid_DSDVDang.DataSource = val.ToList();
            Grid_DSDVDang.DataBind();
        }
    }
    protected void chk_KetNapDang_CheckedChanged(object sender, EventArgs e)
    {
        int selRowIndex = ((GridViewRow)(((CheckBox)sender).Parent.Parent)).RowIndex;
        string madoanvien = Grid_DSDVDang.DataKeys[selRowIndex].Value.ToString();
        CheckBox cb = (CheckBox)Grid_DSDVDang.Rows[selRowIndex].FindControl("chk_KetNapDang");
        if (cb.Checked == true)
        {
            txtmadoanvienvaodang.Value = madoanvien;
            pntcontent.Visible = false;
            pnketnapdang.Visible = true;
            lblthanhcong.Text = "";
        }
        else
        {
            var val = _db.tblGioiThieuChoDangs.Single(r => r.MaDoanVien.Trim() == madoanvien);
            var val1 = _db.tblDoanViens.Single(r => r.MaDonVien.Trim() == madoanvien);
            val.DuocKetNapDang = false;
            val1.DangVien = false;
            try
            {
                _db.SubmitChanges();
            }
            catch { }
            BindGrid();
        }
    }
    protected void btnclose_Click(object sender, EventArgs e)
    {
        pnketnapdang.Visible = false;
        pntcontent.Visible = true;
        BindGrid();
    }
    protected void btnsave_Click(object sender, EventArgs e)
    {
        var val = _db.tblGioiThieuChoDangs.Single(r => r.MaDoanVien.Trim() == txtmadoanvienvaodang.Value);
        var val1 = _db.tblDoanViens.Single(r => r.MaDonVien.Trim() == txtmadoanvienvaodang.Value);
        val1.DangVien = true;
        val.DuocKetNapDang = true;
        val.NgayKetNapDang = CatalogAccess.ConvertToDate(txtvaodang.Text);
        try
        {
            _db.SubmitChanges();
            lblthanhcong.Text = "Đã lưu thành công.";
        }
        catch { }
    }
    protected void Grid_DSDVDang_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "HuyGioiThieu")
        {
            var val = _db.tblGioiThieuChoDangs.Where(r => r.MaDoanVien.Trim() == e.CommandArgument.ToString().Trim());
            if (val.Count() > 0)
            {
                try
                {
                    _db.tblGioiThieuChoDangs.DeleteAllOnSubmit(val);
                    _db.SubmitChanges();
                }
                catch { }
            }
            BindGrid();
        }
    }
}